
Ccay.UI.IForm = {};
﻿/**@description XDialog 组件*/
( function ( ) {
	var config = {

	};
   
    var manager = {
    	
		/** 销毁dialog */
		destory : function(){

		},
		/** 获取form 数据 */
		getValue : function(){
			return this.dom.jsonData()
		},
		/** 填充form */
		setValue : function(data){
			this.dom.jsonData(data);
		},
		/** 重写disable方法 ，禁用form所有可输入元素 */
		disabled:function(enable){
			this.dom.find(":input").each(function(){
				var ctr = $(this);
				 enable ? ctr.removeAttr('disabled') : ctr.attr('disabled', 'disabled');
			})
		},
		/** 设置form子元素为只读 */
		readonly:function(writeable){
			this.dom.find(":input").each(function(){
				var ctr = $(this);
				 writeable ? ctr.removeAttr('readonly') : ctr.attr('readonly', 'readonly');
			})
		},
		/** 重置表单 */
		reset:function(){
			this.dom.find(":input").not(":button,input[type=reset|submit]").ccaySetValue("");
		},
		
		save : function(ajaxOp){
			var ajax = $.extend({},ajaxOp, this.op.operation.save)
			if(ajax.url){
				Ccay.UI.ajax(ajax,this.dom);
			}
		},
		
		reload : function(urlOrAjax){
			var form = this,ajax;
			if(this.op.operation){
				ajax = this.op.operation.fill;
			}
			if(typeof(urlOrAjax) == "string"){
				ajax.url = urlOrAjax;
			}else{
				ajax = $.extend({},ajax,urlOrAjax);
			}
			ajax.success = function(data){
				form.setValue(data);
			};
			Ccay.UI.ajax(ajax,this.dom);
		} 
	};
    
    var flow = {

		init : function( form, op ){
			if(op.data){
				form.dom.jsonData(op.data);
			}
		}
    };
    
    
    var IForm = Ccay.UI.XControl( "IForm", flow, config, manager );
    
	/** dialog 初始化*/
     $.fn.iForm = function ( op ) {
		if(this.is("form")){
      		 new IForm( this, op);
		}
		this.ccayValidate(op);
		Ccay.UI.InitForm(this);
        return this;
    }

} )( Ccay.UI.IForm );

